<?php
/**
 * Created by PhpStorm.
 * User: helloxingtongshi
 * Date: 2016-06-06
 * Time: 14:58
 */
    header("content-Type:text/html;charset=utf-8");
    $filename="F:/PHPstudy/WWW/phplearning/File/message board/message.txt";
    //如果用户提交了，就写入文件，按一定格式写入
    if (isset($_POST['dosubmit'])){
    //    字段的分隔使用||，行的分隔使用【n】
        $message="{$_POST['username']}||{$_POST['title']}||{$_POST['time']}||{$_POST['content']}[n]";
        writemessage($filename,$message);
    }
    if (file_exists($filename)){
        readmessage($filename);
    }
    function writemessage($filename,$message)
    {
        $fp=fopen($filename,"a");
        fwrite($fp,$message);
        fclose($fp);
    }
    function readmessage($filename)
    {
        $message=file_get_contents($filename);
        $message=rtrim($message,"[n]");
        $arrmessage=explode("[n]",$message);
        foreach ($arrmessage as $item) {
            list($username,$title,$time,$content)=explode("||",$item);
            echo "<b>{$username}</b>,".date("Y-m-d H:i:s")."<br><i>{$title}</i>,{$time},<u>{$content}</u><br><hr><br>";
        }
    }
?>